package com.jiyinit.web.core.activiti.service.JyEvectionBill;

import com.jiyinit.web.core.activiti.entity.TabCoreEvectionEntity;
import com.jiyinit.web.core.activiti.entity.TabUserMemershipEntity;
import com.jiyinit.web.core.activiti.enums.ProcessFlowEnum;
import com.jiyinit.web.core.activiti.service.EvectionServiceImpl;
import com.jiyinit.web.core.activiti.service.UserMemershipServiceImpl;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.web.context.ContextLoader;

import java.util.List;

/**
 * Created by user1 on 2018/12/26.
 */
public class EveUserTask3Listener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        UserMemershipServiceImpl userMemershipService = (UserMemershipServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("userMemershipServiceImpl");
        EvectionServiceImpl eveService = (EvectionServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("core.evectionServiceImpl");

        TabCoreEvectionEntity evectionEntity = new TabCoreEvectionEntity();
        evectionEntity.setProcessId(delegateTask.getExecutionId());
        List<TabCoreEvectionEntity> entities = eveService.findBySelective(evectionEntity);

        if (entities.size() > 0) {  //查询下一个节点处理人
            evectionEntity = entities.get(0);
            TabUserMemershipEntity shipEntity = new TabUserMemershipEntity();
            shipEntity.setProcinstKey(ProcessFlowEnum.Evection.getCode());
            //shipEntity.setOrgId(evectionEntity.getOrgId());
            shipEntity.setGroupKey(delegateTask.getTaskDefinitionKey());
            shipEntity.setIsDelete("0");  //未删除
            List<TabUserMemershipEntity> shipList = userMemershipService.findBySelective(shipEntity);
            String mainUserId = "";
            for (TabUserMemershipEntity shipBean : shipList) {
                delegateTask.addCandidateUser(shipBean.getUserId() + "");
                if (shipBean.getIsMain().equals("1")) {
                    mainUserId = shipBean.getUserId() + "";
                }
            }
            delegateTask.setAssignee(mainUserId);
        }

    }
}
